<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <!-- v-on的简写:@ -->
        <cpn @item-click='cpnClick'></cpn>
    </div>

    <template id="cpn">
        <div>
            <button v-for='item in categories' @click='btnClick(item)'>{{item.name}}</button>
        </div>
    </template>

    <script src='../js/vue.js'></script>
    <script>
        const cpn = {
            template: '#cpn',
            data() {
                return {
                    categories: [
                        { id: '1001', name: '热门推荐' },
                        { id: '1002', name: '手机数码' },
                        { id: '1003', name: '家庭家电' },
                        { id: '1004', name: '电脑办公' }
                    ]
                }
            },
            methods: {
                btnClick(item) {
                    //发射 通过自定义事件传给父组件
                    this.$emit('item-click', item);
                }
            }
        }

        const app = new Vue({
            el: '#app',
            data: {

            },
            components: {
                cpn
            },
            methods: {
               //接受发射过来的数据
               cpnClick(item) {
                   console.log(item);
                   
               }
            }
        })
    </script>
</body>

</html>